/*
 * @lc app=leetcode.cn id=1190 lang=java
 *
 * [1190] 反转每对括号间的子串
 */

// @lc code=start
class Solution {
    public String reverseParentheses(String s) {
        StringBuilder sb = new StringBuilder(s);
        for(int i = 0 ,j =s.length()-1;i<j; ){
            char c1 = sb.charAt(i);
            char c2 = sb.charAt(j);
            if(c1 == '(' && c2==')'){
                sb.replace(i, j+1, sb.reverse().substring(i+1,j));
            }else if(c1=='('){
                j--;
            }else if(c2==')'){
                i++;
            }else{
                i++;
                j--;
            }
        }
        for(int i = 0 ;i<sb.length();i++){
            char c = s.charAt(i);
            if(c=='(' || c==')'){
                sb.deleteCharAt(i);
            }
        }
        return sb.toString();
    }
}
// @lc code=end

